package com.mrent.util;

import java.util.Calendar;
import java.util.Date;


public class DateUtil {
	
	/**
	 * 获取日历实例.
	 * 
	 * @param date
	 * @return
	 */
	public static Calendar getCalendar(Date date){
		Calendar c = Calendar.getInstance();
		if(c != null){
			c.setTime( date );
		}
		return c ;
	}
	
	/**
	 * 获取日历实例.
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static Calendar getCalendar(int year,int month,int day){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DATE, day);
		return c;
	}
	
	/**
	 * 获取年份.
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date){
		return getCalendar( date ).get( Calendar.YEAR );
	}
	
	/**
	 * 获取月份.
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date){
		return getCalendar( date ).get( Calendar.MONTH );
	}
	
	/**
	 * 计算两日期的天数差.
	 * 
	 * @param endYear
	 * @param endMonth
	 * @param endDay
	 * @param startYear
	 * @param startMonth
	 * @param startDay
	 * @return
	 */
	public static long getDaysBetween(int endYear,int endMonth,int endDay,int startYear,int startMonth,int startDay){
		Date end = getCalendar(endYear,endMonth,endDay).getTime();
		Date start = getCalendar(startYear,startMonth,startDay).getTime();
		long betweenLong = end.getTime() - start.getTime();
		long betweenDays = betweenLong / 1000 / 60 / 60 / 24;
		return betweenDays;
	}
	
	
	public static void main(String arg[]){
		int endYear = 2011;
		int endMonth = 6;
		int endDay = 1;
		
		int startYear = 2010;
		int startMonth = 6;
		int startDay = 1;
		
		long between = getDaysBetween(endYear,endMonth,endDay,startYear,startMonth,startDay);
		System.out.println(between);
	}
	

}
